﻿using System;
using System.Linq;
using System.Windows.Forms;
using QFlow.Data;

namespace QFlow.Windows.Forms
{
    public partial class NewUser : QFlow.Utils.BaseForm
    {
        bool _New;
        int _UserId;

        /// <summary>
        /// Initializes a new instance of the <see cref="NewUser"/> class.
        /// </summary>
        /// <param name="dialog">if set to <c>true</c> [dialog].</param>
        /// <param name="context">The context.</param>
        public NewUser(bool dialog, QFlowDbContext context)
            : base(context)
        {
            InitializeComponent();
            _New = true;
            if (dialog)
                btnCancel.Visible = true;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="NewUser"/> class.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="context">The context.</param>
        public NewUser(int userId, QFlowDbContext context)
            : base(context)
        {
            InitializeComponent();
            _UserId = userId;
            _New = false;
            btnCancel.Visible = true;
            this.Text = QFlow.Windows.Properties.Resources.FRMEditUser;
        }

        /// <summary>
        /// Handles the Load event of the NewUser control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void NewUser_Load(object sender, EventArgs e)
        {
            cmbRoles.DataSource = this.Context.Roles.ToArray();
            if (!_New)
            {
                User user = this.Context.Users.SingleOrDefault(u => u.UserID == _UserId);
                txtName.Text = user.Username;
                txtPass.Text = user.Password;
                txtConfirmPass.Text = user.Password;
                txtDisplayName.Text = user.DisplayName;
                cmbRoles.SelectedValue = user.RoleID;
            }
        }

        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (superValidator1.Validate())
            {
                try
                {
                    if (_New)
                    {
                        User user = new User();
                        user.Username = txtName.Text;
                        user.Password = txtPass.Text;
                        user.DisplayName = txtDisplayName.Text;
                        user.RoleID = Convert.ToInt32(cmbRoles.SelectedValue);

                        this.Context.Users.InsertOnSubmit(user);
                        this.Context.SubmitChanges();
                    }
                    else
                    {
                        User user = this.Context.Users.SingleOrDefault(u => u.UserID == _UserId);
                        user.Username = txtName.Text;
                        user.Password = txtPass.Text;
                        user.DisplayName = txtDisplayName.Text;
                        user.RoleID = Convert.ToInt32(cmbRoles.SelectedValue);
                        this.Context.SubmitChanges();
                    }
                    PostSave();
                }
                catch
                {
                    MessageBox.Show(QFlow.Windows.Properties.Resources.MBCantSaveChanges, QFlow.Windows.Properties.Resources.MBErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
        }

        /// <summary>
        /// Posts the save.
        /// </summary>
        private void PostSave()
        {
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the btnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handles the FormClosing event of the NewUser control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void NewUser_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (txtName.CausesValidation)
            {
                txtName.CausesValidation = false;
                txtPass.CausesValidation = false;
                txtConfirmPass.CausesValidation = false;
                txtDisplayName.CausesValidation = false;
                this.Close();
            }
        }
    }
}
